﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>避免打开无效页面</title>
    <script type="text/javascript">
        function getURL(url){
            var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//创建XMLHTTP对象
            xmlhttp.open("GET",url,false);  
            xmlhttp.send();
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                return false;
            }
        }
        function test(e,url){
            if(!getURL(e.href)){
                e.href = url;
            }
        }
    </script>
</head>
<body>
<a href="www.baidu.com" onclick="test(this,'www.baidu.com')" >打开测试页</a>
</body>
</html>